<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:仲裁任务
// +----------------------------------------------------------------------


namespace app\admin\controller;
use app\BackendController;
use app\admin\model\MissionDissent as cmodel;
use think\facade\Cache;
use support\Request;
use think\facade\Db;

class MissionDissentController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $status         = cmodel::getStatus();
        $status_options = output_select($status, []);
        return view('missiondissent/index', compact('status_options'));
    }

    /**
     * 列表json数据
     * @param  Request $request [请求变量]
     * @return [type]           [json数据]
     */
    public function listData(Request $request)
    {
        $limit = $request->get('limit', 10);
        $w     = [];

        if ($request->get('title')) {
            $w[] = ['b.title', 'like', '%' . $request->get('title') . '%'];

        }
        if ($request->get('content')) {
            $w[] = ['a.content', 'like', '%' . $request->get('content') . '%'];

        }

        if ($request->get('remark')) {
            $w[] = ['a.remark', 'like', '%' . $request->get('remark') . '%'];

        }

        if ($request->get('create_time')) {
            $w[] = ['a.create_time', '>=',  strtotime($request->get('create_time'))];

        }

        if ($request->get('u_id')) {
            $w[] = ['a.u_id', '=', $request->get('u_id')];

        }
        if (null != $request->get('status')) {
            $w[] = ['a.status', '=', $request->get('status')];

        }

        $q = cmodel::alias('a')->join("mission_description b", "a.mission_id=b.mission_id ", 'left')->field("a.*,b.title as mission_title")->where($w);
        $q->order("id","desc");
        $ls = $q->paginate(['list_rows' => $limit, 'request' => $request->all()]);

        $res    = $ls->items();
        $status = cmodel::getStatus();
        $res    = array_map(function ($v) use ($status) {

            $v->status_txt = $status[$v->status];
            $v->user_info  = $v->user_info;
            $v->profile    = $v->user_info->profile;
            return $v;
        }, $res);

        return json(['code' => $ls->total() > 0 ? 0 : 201, 'msg' => '无数据', 'count' => $ls->total(), 'data' => $res]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id = $request->get("id");
        $data = cmodel::find($id);     
        $status         = cmodel::getStatus();
        $status_options = output_select($status, $data->status);
        return view('missiondissent/edit', compact('data', 'status_options'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $id  = $request->get("id");
        $post  = $request->post();
        $valid = new \app\admin\validate\MissionDissent;
        $r     = $valid->check($post);

        $cmodel = new cmodel;
        $info   = $cmodel->where('id', $id)->find();
        
        if (!$r) {
            $msg = ['status' => -1, 'msg' => $valid->getError()];
        } else {

            $info->allowField(['remark', 'status'])->save($post);           
            if($info->status==1){
                \app\api\model\Mission::update(['status'=>5],['mission_id'=>$info->mission_id]);
                //增加信用分
                \app\api\library\Credit::sceneLog($info->u_id, 'arbitration_win',['uk'=>$id]);
            }

            $msg = ['status' => 1, 'msg' => '保存成功'];
        }
        return json($msg);

    }

}
